package com.cyf.app.service.bean;

import com.cyf.db.bean.Parking;
import com.cyf.db.mapper.BookingInfoMapper;
import com.cyf.db.mapper.ParkingLockInfoMapper;
import com.cyf.db.mapper.ParkingMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.io.PipedReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车场中可用的锁的数量
 * Created by cage on 2017/4/10.
 */
@Component
public class EnableLockCountMap implements InitializingBean {
    /**
     * 各个停车场可用的地锁数量（key：车场id， value：可用地锁数量）
     */
    private static Map<Integer, Integer> enableLockCountMap = new HashMap<>();

    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private BookingInfoMapper bookingInfoMapper;
    @Autowired
    private ParkingLockInfoMapper parkingLockInfoMapper;

    /**
     * 获取车场可用的地锁数量
     * @param key   车场id
     * @return  int
     */
    public int getEnableLockCount(Integer key) {
        return enableLockCountMap.get(key) == null ? 0 : enableLockCountMap.get(key);
    }

    /**
     * 车位预约，车场可用的地锁数量 - 1
     * @param key  车场id
     */
    public void booking(Integer key) throws BookingFailException {
        synchronized (EnableLockCountMap.class) {
            Integer count = enableLockCountMap.get(key);
            if (count == null || count == 0) {
                throw new BookingFailException();
            }
            enableLockCountMap.put(key, --count);
        }
    }

    /**
     * 取消车位预约，车场可用的地锁数量 + 1
     * @param key  车场id
     */
    public void cancelBooking(Integer key) throws BookingFailException {
        synchronized (EnableLockCountMap.class) {
            synchronized (EnableLockCountMap.class) {
                Integer count = enableLockCountMap.get(key);
                if (count == null) {
                    throw new BookingFailException();
                }
                enableLockCountMap.put(key, ++count);
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
//        init();
    }

    private void init() {
        enableLockCountMap = getEnableLockCountMap();
    }

    private Map<Integer, Integer> getEnableLockCountMap() {
        Map<Integer, Integer> enableLockCountMap = new HashMap<>();
        // TODO: 2018/8/13  parkingMapper.getParkingsByUseBooking(Parking.ENABLE_BOOKING);
        List<Parking> parkings = null;
        if (parkings != null && parkings.size() > 0) {
            for (Parking parking : parkings) {
                Integer usedCount = bookingInfoMapper.getCountByParkingId(parking.getId());
                Integer count = parkingLockInfoMapper.getCountByParkingId(parking.getId());
                int value = 0;
                if (count != null) {
                    usedCount = usedCount == null ? 0 : usedCount;
                    if (count < usedCount) {
                        throw new RuntimeException("已经预约车位数量("+ usedCount +") > 总预约车位数("+ count +")！");
                    }
                    value = count - usedCount;
                }
                enableLockCountMap.put(parking.getId(), value);
            }
        }
        return enableLockCountMap;
    }

    public void update(){
        init();
    }
}
